{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ogr, gdal\n",
    "import os\n",
    "import pandas as pd\n",
    "path = os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver = ogr.GetDriverByName(\"ESRI Shapefile\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "xzqhshp = path +\"/china/\"+ \"cn.shp\"\n",
    "outfile = path + \"/china.shp\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### gdal.SetConfigOption(\"SHAPE_ENCODING\", \"CP936\")\n",
    "### 这句话需要放在copy之前，或者是创建shapefile之前，否则会出现乱码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "xzqh = driver.Open(xzqhshp)\n",
    "gdal.SetConfigOption(\"SHAPE_ENCODING\", \"CP936\")\n",
    "cp = driver.CopyDataSource(xzqh, outfile)\n",
    "xzqh.Destroy()\n",
    "cp.Release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "outputfile = path + \"/china_10000.shp\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = driver.Open(xzqhshp)\n",
    "if os.access( outputfile, os.F_OK ):\n",
    "    driver.DeleteDataSource( outputfile )\n",
    "layer = ds.GetLayer()\n",
    "layer.SetAttributeFilter(\"GDP_2005 > 10000\")\n",
    "gdal.SetConfigOption(\"SHAPE_ENCODING\", \"CP936\")\n",
    "newds = driver.CreateDataSource(outputfile)\n",
    "cpl  = newds.CopyLayer(layer, 'abcd')\n",
    "newds.Destroy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "D:\\workspace\\DevWork\\PyExample\\exam4/china_10000.shp\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AREA</th>\n",
       "      <th>CATEGORIES</th>\n",
       "      <th>CPI_2009</th>\n",
       "      <th>Code</th>\n",
       "      <th>FIRST_NAME</th>\n",
       "      <th>GDP_2005</th>\n",
       "      <th>GDP_2006</th>\n",
       "      <th>GDP_2007</th>\n",
       "      <th>GDP_2008</th>\n",
       "      <th>GDP_2009</th>\n",
       "      <th>Pop_1990</th>\n",
       "      <th>Pop_1995</th>\n",
       "      <th>Pop_1999</th>\n",
       "      <th>Pop_2000</th>\n",
       "      <th>Pop_2005</th>\n",
       "      <th>Pop_2009</th>\n",
       "      <th>Pop_Birth_</th>\n",
       "      <th>Pop_Death_</th>\n",
       "      <th>Shape</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.003209e+11</td>\n",
       "      <td>2.0</td>\n",
       "      <td>11993.0</td>\n",
       "      <td>320000</td>\n",
       "      <td>江苏</td>\n",
       "      <td>18598.69</td>\n",
       "      <td>21742.05</td>\n",
       "      <td>26018.48</td>\n",
       "      <td>30981.98</td>\n",
       "      <td>34457.30</td>\n",
       "      <td>6705.6519</td>\n",
       "      <td>7066.0</td>\n",
       "      <td>7213.0</td>\n",
       "      <td>7438.0</td>\n",
       "      <td>7475.0</td>\n",
       "      <td>7725.0</td>\n",
       "      <td>9.55</td>\n",
       "      <td>6.56</td>\n",
       "      <td>POLYGON ((119.78779842 34.4813239650001,120.29...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.009434e+11</td>\n",
       "      <td>2.0</td>\n",
       "      <td>15790.0</td>\n",
       "      <td>330000</td>\n",
       "      <td>浙江</td>\n",
       "      <td>13417.68</td>\n",
       "      <td>15718.47</td>\n",
       "      <td>18753.73</td>\n",
       "      <td>21462.69</td>\n",
       "      <td>22990.35</td>\n",
       "      <td>4144.5930</td>\n",
       "      <td>4319.0</td>\n",
       "      <td>4475.0</td>\n",
       "      <td>4677.0</td>\n",
       "      <td>4898.0</td>\n",
       "      <td>5180.0</td>\n",
       "      <td>10.22</td>\n",
       "      <td>6.75</td>\n",
       "      <td>POLYGON ((120.12826222 30.947050775,120.516654...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.542384e+11</td>\n",
       "      <td>2.0</td>\n",
       "      <td>10494.0</td>\n",
       "      <td>370000</td>\n",
       "      <td>山东</td>\n",
       "      <td>18366.87</td>\n",
       "      <td>21900.19</td>\n",
       "      <td>25776.91</td>\n",
       "      <td>30933.28</td>\n",
       "      <td>33896.65</td>\n",
       "      <td>8439.2827</td>\n",
       "      <td>8705.0</td>\n",
       "      <td>8883.0</td>\n",
       "      <td>9079.0</td>\n",
       "      <td>9248.0</td>\n",
       "      <td>9470.3</td>\n",
       "      <td>11.70</td>\n",
       "      <td>6.47</td>\n",
       "      <td>POLYGON ((122.214118076 36.907911525,122.24096...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.650694e+11</td>\n",
       "      <td>2.0</td>\n",
       "      <td>6607.0</td>\n",
       "      <td>410000</td>\n",
       "      <td>河南</td>\n",
       "      <td>10587.42</td>\n",
       "      <td>12362.79</td>\n",
       "      <td>15012.46</td>\n",
       "      <td>18018.53</td>\n",
       "      <td>19480.46</td>\n",
       "      <td>8550.9535</td>\n",
       "      <td>9100.0</td>\n",
       "      <td>9387.0</td>\n",
       "      <td>9256.0</td>\n",
       "      <td>9380.0</td>\n",
       "      <td>9487.0</td>\n",
       "      <td>11.45</td>\n",
       "      <td>6.28</td>\n",
       "      <td>POLYGON ((114.523150095 36.174611795,114.90826...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.760488e+11</td>\n",
       "      <td>3.0</td>\n",
       "      <td>15291.0</td>\n",
       "      <td>440000</td>\n",
       "      <td>广东</td>\n",
       "      <td>22557.37</td>\n",
       "      <td>26587.76</td>\n",
       "      <td>31777.01</td>\n",
       "      <td>36796.71</td>\n",
       "      <td>39482.56</td>\n",
       "      <td>6282.9236</td>\n",
       "      <td>6868.0</td>\n",
       "      <td>7270.0</td>\n",
       "      <td>8642.0</td>\n",
       "      <td>9194.0</td>\n",
       "      <td>9638.0</td>\n",
       "      <td>11.78</td>\n",
       "      <td>5.70</td>\n",
       "      <td>POLYGON ((114.25986752 25.2930331750001,114.39...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.855880e+11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7193.0</td>\n",
       "      <td>130000</td>\n",
       "      <td>河北</td>\n",
       "      <td>10012.11</td>\n",
       "      <td>11467.60</td>\n",
       "      <td>13607.32</td>\n",
       "      <td>16011.97</td>\n",
       "      <td>17235.48</td>\n",
       "      <td>6108.2439</td>\n",
       "      <td>6437.0</td>\n",
       "      <td>6614.0</td>\n",
       "      <td>6744.0</td>\n",
       "      <td>6851.0</td>\n",
       "      <td>7034.4</td>\n",
       "      <td>12.93</td>\n",
       "      <td>6.32</td>\n",
       "      <td>MULTIPOLYGON (((118.91233351064 39.12308411298...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           AREA  CATEGORIES  CPI_2009    Code FIRST_NAME  GDP_2005  GDP_2006  \\\n",
       "0  1.003209e+11         2.0   11993.0  320000         江苏  18598.69  21742.05   \n",
       "1  1.009434e+11         2.0   15790.0  330000         浙江  13417.68  15718.47   \n",
       "2  1.542384e+11         2.0   10494.0  370000         山东  18366.87  21900.19   \n",
       "3  1.650694e+11         2.0    6607.0  410000         河南  10587.42  12362.79   \n",
       "4  1.760488e+11         3.0   15291.0  440000         广东  22557.37  26587.76   \n",
       "5  1.855880e+11         1.0    7193.0  130000         河北  10012.11  11467.60   \n",
       "\n",
       "   GDP_2007  GDP_2008  GDP_2009   Pop_1990  Pop_1995  Pop_1999  Pop_2000  \\\n",
       "0  26018.48  30981.98  34457.30  6705.6519    7066.0    7213.0    7438.0   \n",
       "1  18753.73  21462.69  22990.35  4144.5930    4319.0    4475.0    4677.0   \n",
       "2  25776.91  30933.28  33896.65  8439.2827    8705.0    8883.0    9079.0   \n",
       "3  15012.46  18018.53  19480.46  8550.9535    9100.0    9387.0    9256.0   \n",
       "4  31777.01  36796.71  39482.56  6282.9236    6868.0    7270.0    8642.0   \n",
       "5  13607.32  16011.97  17235.48  6108.2439    6437.0    6614.0    6744.0   \n",
       "\n",
       "   Pop_2005  Pop_2009  Pop_Birth_  Pop_Death_  \\\n",
       "0    7475.0    7725.0        9.55        6.56   \n",
       "1    4898.0    5180.0       10.22        6.75   \n",
       "2    9248.0    9470.3       11.70        6.47   \n",
       "3    9380.0    9487.0       11.45        6.28   \n",
       "4    9194.0    9638.0       11.78        5.70   \n",
       "5    6851.0    7034.4       12.93        6.32   \n",
       "\n",
       "                                               Shape  \n",
       "0  POLYGON ((119.78779842 34.4813239650001,120.29...  \n",
       "1  POLYGON ((120.12826222 30.947050775,120.516654...  \n",
       "2  POLYGON ((122.214118076 36.907911525,122.24096...  \n",
       "3  POLYGON ((114.523150095 36.174611795,114.90826...  \n",
       "4  POLYGON ((114.25986752 25.2930331750001,114.39...  \n",
       "5  MULTIPOLYGON (((118.91233351064 39.12308411298...  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(outputfile)\n",
    "dataSource = driver.Open(outputfile, 0)\n",
    "layer = dataSource.GetLayer()\n",
    "lyd = layer.GetLayerDefn()\n",
    "fields = [lyd.GetFieldDefn(i).GetName() for i in range(lyd.GetFieldCount())]\n",
    "layer.ResetReading()\n",
    "feDict = []\n",
    "for feature in layer:\n",
    "    f = {}\n",
    "    for field in fields:\n",
    "        val = feature.GetField(field)\n",
    "        if isinstance(val,str):\n",
    "            val = val.encode('gbk').decode('utf-8')\n",
    "        f[field] = val\n",
    "    geo = feature.GetGeometryRef()\n",
    "    f[\"Shape\"] = geo.ExportToWkt()\n",
    "    feDict.append(f)\n",
    "    feature.Destroy()\n",
    "layer.ResetReading()\n",
    "dataSource.Destroy()\n",
    "pd.DataFrame(feDict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
